import logging

from checker import BaseChecker

class FileGenStatusChecker(BaseChecker):
    def __init__(self,db_service):
        super(FileGenStatusChecker,self).__init__(db_service)
        self.logger=logging.getLogger("FileGenStatusChecker")
        self.batch_desc="GENERATE FILE"

    def check_gen_status(self):
        flag_array=[]
        batch_date_array=[]
        with self.db_service.get_instance("gmrmbatch") as db_session:
            fail_count=self.load_bat_batch_gen_fail_count(db_session=db_session)
            if fail_count==0:
                self.logger.info("All files generated successfully")
                flag=True
            else:
                self.logger.info("Error status . Number of failed failes:{}".format(fail_count))
                fail_file_list=self.load_bat_batch_gen_fail_info(db_session=db_session)
                for index,row in fail_file_list.iterrows():
                    self.logger.info("file_name:{} . file_dest_dir_name:{} ,status:{}".format(row["FILE_NAME"],row["DEST_DIR_NAME"],row["STATUS"]))
                flag=False 
        flag_array.append(flag)
        return self.analysis_check_result(batch_date_array,flag_array,self.batch_desc)
    
    def load_bat_batch_gen_fail_count(self,db_session):
        """
        获取不成功的文件生成实例数量
        """
        sql="SELECT COUNT(1) AS COUNT FROM GMRMBATCH.BAT_FILE_GEN_INSTANCE T WHERE T.STATUS IN('1','2','4','5','6','8','10')"
        df=db_session.get_dataframe_by_sql(sql)
        return df.loc[0,"COUNT"]
    
    def load_bat_batch_gen_fail_info(self,db_session):
        """
        获取不成功的文件信息
        """
        sql="SELECT T.FILE_NAME,T.DEST_DIR_NAME,T.STATUS FROM GMRMBATCH.BAT_FILE_GEN_INSTANCE T WHERE T.STATUS IN('1','2','4','5','6','8','10')"
        return db_session.get_dataframe_by_sql(sql)